package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.annottation.LogAnnottion;
import com.baizhi.cmfz.entity.Article;
import com.baizhi.cmfz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
       @RequestMapping("/selectByPageArticle")
       @ResponseBody
       @AddCacheAnnotation
    public Map selectByPageArticle(Integer page,Integer limit,String name){
        return  articleService.selectByPageArticle(page,limit,name);
    }
    @ResponseBody
    @RequestMapping("/insertArticle")
    @LogAnnottion(type = "insert", value = "添加文章")
    @DeleteCacheAnnotation
    public  Map insertArticle(HttpServletRequest request, Article article, MultipartFile file) throws IOException {

           if(file != null){
               String realPath = request.getServletContext().getRealPath("/img");
               String originalFilename = file.getOriginalFilename();
               String uuid = UUID.randomUUID().toString();
               String newName=uuid+"_"+originalFilename;

               file.transferTo(new File(realPath+"/"+newName));
               article.setArticleImage(newName);
           }
           article.setArticleDate(new Date());
           article.setGuruId(1);
       return articleService.insertArticle(article);
    }

    @RequestMapping("/updateArticle")
    @ResponseBody
    @LogAnnottion(type = "update", value = "修改文章")
    @DeleteCacheAnnotation
    public  Map  updateArticle(Article article,MultipartFile file,HttpServletRequest request) throws IOException {


           if(file != null){
               String realPath = request.getServletContext().getRealPath("/img");
               String newName=UUID.randomUUID().toString()+"_"+file.getOriginalFilename();
                file.transferTo(new File(realPath+"/"+newName));
            article.setArticleImage(newName);
           }

           return articleService.updateArticle(article);

    }

    @ResponseBody
    @RequestMapping("/selectOneArticle")
    @LogAnnottion(type = "select", value = "查询一个文章")
    @AddCacheAnnotation
    public  Article selectOneArticle(Integer id){

           return  articleService.selectOne(id);
    }

    @RequestMapping("/deleteArticle")
    @ResponseBody
    @LogAnnottion(type = "delete", value = "删除一个文章")
    @DeleteCacheAnnotation
    public  Map deleteArticle(Integer id){

           return  articleService.deleteArticle(id);
    }


}
